package com.appcenter.application.mapper;

import com.appcenter.application.dto.roles.RoleDto;
import com.appcenter.application.dto.roleusers.RoleUserDto;
import com.appcenter.application.input.roleuser.GetRoleUsersInput;
import com.appcenter.application.input.roleuser.GetRoleUsersServiceInput;
import com.appcenter.application.model.RoleUsers;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;

@org.apache.ibatis.annotations.Mapper
@Repository
public interface RoleUsersMapper extends Mapper<RoleUsers> {


    @Delete("delete from roleusers where ValidStatus=1 and UserId=#{UserId}")
    public int deleteRoleUserByUserId(String UserId);



    @Select("<script>" +
            "select a.*,d.AppName,b.RoleName,c.UserCode,c.UserName from roleusers a " +
            " inner JOIN roles b on a.RoleId=b.Id" +
            " inner join users c on c.Id=a.UserId" +
            " inner JOIN apps d on b.AppId=d.Id" +
            " where a.ValidStatus=1 and b.ValidStatus=1 and d.ValidStatus=1 and c.VersionNo=#{versionNo}" +
            "" +
            " <when test='keyword!=null'>" +
            " AND (c.UserCode like concat('%',#{keyword},'%') or c.UserName like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            " <when test='appId!=null'>" +
            " AND b.AppId=#{appId}" +
            " </when>" +
            "" +
            " <when test='roleId!=null'>" +
            " AND a.RoleId=#{roleId}" +
            " </when>" +
            "" +
            " <when test='appIds!=null'>" +
            " and b.AppId in " +
            " <foreach collection='appIds' item='appId' open='(' separator=',' close=')'>" +
            " #{appId}" +
            " </foreach>" +
            " </when>" +
            "</script>")
    List<RoleUserDto> searchRoleUser(GetRoleUsersInput input);

    @Select("<script>" +
            "SELECT roleusers.Id,apps.AppName,users.Id as userId,users.UserCode,users.UserName,roles.Id as roleId,roles.RoleName from roleUsers"+
            " INNER JOIN users  ON users.Id=roleusers.UserId"+
            " INNER JOIN roles ON roles.Id=roleusers.RoleId "+
            " INNER JOIN apps ON apps.Id=roles.AppId"+
            " where 1=1"+
            "" +
            " <when test='keyword!=null'>" +
            " AND ( Users.UserCode like concat('%',#{keyword},'%') or Users.UserName like concat('%',#{keyword},'%'))" +
            " </when>" +
            "" +
            " <when test='appId!=null'>" +
            " AND Roles.AppId=#{appId}"+
            " </when>" +
            "" +
            " <when test='roleId!=null'>" +
            " AND Roles.Id=#{roleId}"+
            " </when>" +
            "" +
            "  ORDER BY UserCode"+
            "</script>")
    List<RoleUserDto> getRoleUsers(GetRoleUsersServiceInput input);


    @Select("select b.* from roleusers a " +
            " inner join roles b on a.roleid=b.id " +
            " inner join apps c on b.appid=c.id " +
            " where a.userid= #{userId} " +
            " and c.appCode = #{appCode} " +
            " and a.validStatus=1 " +
            " and b.ValidStatus=1 " +
            " and c.ValidStatus=1")
    public List<RoleDto> getRolesByUserId(@Param("userId") String userId, @Param("appCode") String appCode,@Param("versionNo") Long versionNo);


}
